{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "182738d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 创建含缺失值的DataFrame\n",
    "df = pd.DataFrame({\n",
    "    'A': [1, 2, np.nan, 4],\n",
    "    'B': [3, 4, 4, 5],\n",
    "    'C': [5, 6, 7, 8],\n",
    "    'D': [7, 5, np.nan, np.nan]\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d9e57b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.0</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.0</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A  B  C    D\n",
       "0  1.0  3  5  7.0\n",
       "1  2.0  4  6  5.0\n",
       "2  NaN  4  7  NaN\n",
       "3  4.0  5  8  NaN"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6b041678",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       A      B      C      D\n",
       "0  False  False  False  False\n",
       "1  False  False  False  False\n",
       "2   True  False  False   True\n",
       "3  False  False  False   True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.isna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c84eb40c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       A     B     C      D\n",
       "0   True  True  True   True\n",
       "1   True  True  True   True\n",
       "2  False  True  True  False\n",
       "3   True  True  True  False"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.notna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1a4654ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.0</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.0</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A  B  C    D\n",
       "0  1.0  3  5  7.0\n",
       "1  2.0  4  6  5.0\n",
       "3  4.0  5  8  NaN"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_drop = df.dropna(thresh=3,inplace = False)\n",
    "df_drop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2c4761f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.0</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.3</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.0</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A  B  C    D\n",
       "0  1.0  3  5  7.0\n",
       "1  2.0  4  6  5.0\n",
       "2  2.3  4  7  6.0\n",
       "3  4.0  5  8  6.0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mean_A = np.around(df[\"A\"].mean(),1)\n",
    "mean_D = np.around(df[\"D\"].mean(),1)\n",
    "df_fill = df.fillna({\"A\":mean_A,\"D\":mean_D})\n",
    "df_fill"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e351158a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.0</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3.0</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.0</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A  B  C    D\n",
       "0  1.0  3  5  7.0\n",
       "1  2.0  4  6  5.0\n",
       "2  3.0  4  7  5.0\n",
       "3  4.0  5  8  5.0"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_interpolate = df.interpolate(method='linear')\n",
    "df_interpolate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0aa62d9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>height</th>\n",
       "      <th>gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>刘婷婷</td>\n",
       "      <td>24</td>\n",
       "      <td>162</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>王淼</td>\n",
       "      <td>23</td>\n",
       "      <td>165</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>彭岩</td>\n",
       "      <td>29</td>\n",
       "      <td>175</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>刘华</td>\n",
       "      <td>22</td>\n",
       "      <td>175</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>刘华</td>\n",
       "      <td>22</td>\n",
       "      <td>175</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>周华</td>\n",
       "      <td>27</td>\n",
       "      <td>178</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>周华</td>\n",
       "      <td>31</td>\n",
       "      <td>182</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  name  age  height gender\n",
       "0  刘婷婷   24     162      女\n",
       "1   王淼   23     165      女\n",
       "2   彭岩   29     175      男\n",
       "3   刘华   22     175      男\n",
       "4   刘华   22     175      男\n",
       "5   周华   27     178      男\n",
       "6   周华   31     182      男"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person = pd.DataFrame({\n",
    "    'name': [\"刘婷婷\", \"王淼\", \"彭岩\", \"刘华\", \"刘华\", \"周华\", \"周华\"],\n",
    "    'age': [24, 23, 29, 22, 22, 27, 31],\n",
    "    'height': [162, 165, 175, 175, 175, 178, 182],\n",
    "    'gender': ['女', '女', '男', '男', '男', '男', '男']\n",
    "})\n",
    "person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1d4b9112",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    False\n",
       "1    False\n",
       "2    False\n",
       "3    False\n",
       "4     True\n",
       "5    False\n",
       "6     True\n",
       "dtype: bool"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person.duplicated(subset=['name'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a9a6cb1b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>height</th>\n",
       "      <th>gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>刘婷婷</td>\n",
       "      <td>24</td>\n",
       "      <td>162</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>王淼</td>\n",
       "      <td>23</td>\n",
       "      <td>165</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>彭岩</td>\n",
       "      <td>29</td>\n",
       "      <td>175</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>刘华</td>\n",
       "      <td>22</td>\n",
       "      <td>175</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>周华</td>\n",
       "      <td>27</td>\n",
       "      <td>178</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  name  age  height gender\n",
       "0  刘婷婷   24     162      女\n",
       "1   王淼   23     165      女\n",
       "2   彭岩   29     175      男\n",
       "3   刘华   22     175      男\n",
       "4   周华   27     178      男"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person_c = person.drop_duplicates(subset=['name'], keep='first', ignore_index=True)\n",
    "person_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f40fdf48",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>height</th>\n",
       "      <th>gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>刘婷婷</td>\n",
       "      <td>24</td>\n",
       "      <td>162</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>王淼</td>\n",
       "      <td>23</td>\n",
       "      <td>165</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>彭岩</td>\n",
       "      <td>29</td>\n",
       "      <td>175</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>刘华</td>\n",
       "      <td>22</td>\n",
       "      <td>175</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>刘华</td>\n",
       "      <td>22</td>\n",
       "      <td>175</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>周华</td>\n",
       "      <td>27</td>\n",
       "      <td>178</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>周华</td>\n",
       "      <td>31</td>\n",
       "      <td>182</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  name  age  height gender\n",
       "0  刘婷婷   24     162      女\n",
       "1   王淼   23     165      女\n",
       "2   彭岩   29     175      男\n",
       "3   刘华   22     175      男\n",
       "4   刘华   22     175      男\n",
       "5   周华   27     178      男\n",
       "6   周华   31     182      男"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person2 = pd.DataFrame({\n",
    "    'name': [\"刘婷婷\", \"王淼\", \"彭岩\", \"刘华\", \"刘华\", \"周华\", \"周华\"],\n",
    "    'age': [24, 23, 29, 22, 22, 27, 31],\n",
    "    'height': [162, 165, 175, 175, 175, 178, 182],\n",
    "    'gender': ['女', '女', '男', '男', '男', '男', '男']\n",
    "})\n",
    "person2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "191d85f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试数据：\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>num</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>3.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>3.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>3.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>3.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>4.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>3.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>4.1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    num\n",
       "0   1.1\n",
       "1   2.0\n",
       "2   2.3\n",
       "3   3.0\n",
       "4   3.1\n",
       "5   3.3\n",
       "6   3.0\n",
       "7   3.4\n",
       "8   3.4\n",
       "9   3.2\n",
       "10  4.0\n",
       "11  4.1\n",
       "12  8.0\n",
       "13  3.2\n",
       "14  4.1"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([1.1, 2, 2.3, 3, 3.1, 3.3, 3, 3.4, 3.4, 3.2, 4, 4.1, 8, 3.2, 4.1])\n",
    "df2= pd.DataFrame(arr,columns=['num'])\n",
    "print('测试数据：')\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "96758bad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "3σ原则检测出的异常值：\n",
      "0     1.1\n",
      "1     2.0\n",
      "2     2.3\n",
      "3     3.0\n",
      "4     3.1\n",
      "5     3.3\n",
      "6     3.0\n",
      "7     3.4\n",
      "8     3.4\n",
      "9     3.2\n",
      "10    4.0\n",
      "11    4.1\n",
      "13    3.2\n",
      "14    4.1\n",
      "Name: num, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "def three_sigma(ser):\n",
    "    \"\"\"\n",
    "    \"\"\"\n",
    "    mean_data = ser.mean() \n",
    "    std_data = ser.std()\n",
    "    # 异常值规则：小于μ-3σ或大于μ+3σ\n",
    "    rule = (ser<(mean_data - 3*std_data))|(ser<(mean_data + 3*std_data))\n",
    "    #获取异常值的索引和值\n",
    "    index = np.arange(ser.shape[0])[rule]\n",
    "    outliers = ser.iloc[index ]\n",
    "    return outliers\n",
    "outliers_sigma = three_sigma(df2['num'])\n",
    "print(\"\\n3σ原则检测出的异常值：\")\n",
    "print(outliers_sigma)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "8e5b2f22",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "箱形图检测出的异常值：\n",
      "0     1.1\n",
      "12    8.0\n",
      "Name: num, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "def box_outliers(ser):\n",
    "    new_ser = ser.sort_values()  # 排序\n",
    "    n = new_ser.count()\n",
    "    # 计算Q1（下四分位数）和Q3（上四分位数）\n",
    "    if n % 2 == 0:\n",
    "        Q3 = new_ser[int(n/2):].median()\n",
    "        Q1 = new_ser[:int(n/2)].median()\n",
    "    else:\n",
    "        Q3 = new_ser[int((n-1)/2):].median()\n",
    "        Q1 = new_ser[:int((n-1)/2)].median()\n",
    "    IQR = round(Q3 - Q1, 1)  # 四分位数间距\n",
    "    # 异常值规则：小于Q1-1.5IQR或大于Q3+1.5IQR\n",
    "    rule = (ser < (Q1 - 1.5 * IQR)) | (ser > (Q3 + 1.5 * IQR))\n",
    "    index = np.arange(ser.shape[0])[rule]\n",
    "    outliers = ser.iloc[index]\n",
    "    return outliers\n",
    "\n",
    "# 用箱形图检测异常值\n",
    "outliers_box = box_outliers(df2['num'])\n",
    "print(\"\\n箱形图检测出的异常值：\")\n",
    "print(outliers_box)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8683993c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "左表（df_left）：\n",
      "  key   B\n",
      "0  K0  B0\n",
      "1  K1  B1\n",
      "2  K2  B2\n",
      "3  K4  B4\n",
      "\n",
      "右表（df_right）：\n",
      "  key   A   C   D\n",
      "0  K0  A0  C0  D0\n",
      "1  K1  A1  C1  D1\n",
      "2  K2  A2  C2  D2\n",
      "3  K3  A3  C3  D3\n"
     ]
    }
   ],
   "source": [
    "df_left = pd.DataFrame({\n",
    "    'key': [\"K0\", \"K1\", \"K2\", \"K4\"],\n",
    "    'B': [\"B0\", \"B1\", \"B2\", \"B4\"]\n",
    "})\n",
    "df_right = pd.DataFrame({\n",
    "    'key': [\"K0\", \"K1\", \"K2\", \"K3\"],\n",
    "    'A': [\"A0\", \"A1\", \"A2\", \"A3\"],\n",
    "    'C': [\"C0\", \"C1\", \"C2\", \"C3\"],\n",
    "    'D': [\"D0\", \"D1\", \"D2\", \"D3\"]\n",
    "})\n",
    "\n",
    "print(\"左表（df_left）：\")\n",
    "print(df_left)\n",
    "print(\"\\n右表（df_right）：\")\n",
    "print(df_right)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2b00fdc5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>B</th>\n",
       "      <th>A</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>K0</td>\n",
       "      <td>B0</td>\n",
       "      <td>A0</td>\n",
       "      <td>C0</td>\n",
       "      <td>D0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>K1</td>\n",
       "      <td>B1</td>\n",
       "      <td>A1</td>\n",
       "      <td>C1</td>\n",
       "      <td>D1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>K2</td>\n",
       "      <td>B2</td>\n",
       "      <td>A2</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key   B   A   C   D\n",
       "0  K0  B0  A0  C0  D0\n",
       "1  K1  B1  A1  C1  D1\n",
       "2  K2  B2  A2  C2  D2"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "merge_inner = pd.merge(df_left,df_right,on='key',how='inner')\n",
    "merge_inner\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c74a2ece",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key   B   A   C   D\n",
      "0  K0  B0  A0  C0  D0\n",
      "1  K1  B1  A1  C1  D1\n",
      "2  K2  B2  A2  C2  D2\n"
     ]
    }
   ],
   "source": [
    "print(merge_inner)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "68ce75b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key   B    A    C    D\n",
      "0  K0  B0   A0   C0   D0\n",
      "1  K1  B1   A1   C1   D1\n",
      "2  K2  B2   A2   C2   D2\n",
      "3  K4  B4  NaN  NaN  NaN\n",
      "  key   A   C   D    B\n",
      "0  K0  A0  C0  D0   B0\n",
      "1  K1  A1  C1  D1   B1\n",
      "2  K2  A2  C2  D2   B2\n",
      "3  K3  A3  C3  D3  NaN\n"
     ]
    }
   ],
   "source": [
    "merge_left1 = pd.merge(df_left,df_right,on='key',how='left')\n",
    "print(merge_left1)\n",
    "merge_left2 = pd.merge(df_right,df_left,on='key',how='left')\n",
    "print(merge_left2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2a39d605",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "人员表1：\n",
      "  name  age  height gender\n",
      "0  张三丰  100     180      男\n",
      "1  张无忌   20     185      男\n",
      "\n",
      "人员表2：\n",
      "  name  age  height gender\n",
      "0   赵敏   19     165      女\n",
      "1  周芷若   21     168      女\n"
     ]
    }
   ],
   "source": [
    "person1 = pd.DataFrame({\n",
    "    'name': [\"张三丰\", \"张无忌\"],\n",
    "    'age': [100, 20],\n",
    "    'height': [180, 185],\n",
    "    'gender': ['男', '男']\n",
    "})\n",
    "person2 = pd.DataFrame({\n",
    "    'name': [\"赵敏\", \"周芷若\"],\n",
    "    'age': [19, 21],\n",
    "    'height': [165, 168],\n",
    "    'gender': ['女', '女']\n",
    "})\n",
    "\n",
    "print(\"人员表1：\")\n",
    "print(person1)\n",
    "print(\"\\n人员表2：\")\n",
    "print(person2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "abb3a5f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A_name</th>\n",
       "      <th>A_age</th>\n",
       "      <th>A_height</th>\n",
       "      <th>A_gender</th>\n",
       "      <th>B_name</th>\n",
       "      <th>B_age</th>\n",
       "      <th>B_height</th>\n",
       "      <th>B_gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>张三丰</td>\n",
       "      <td>100</td>\n",
       "      <td>180</td>\n",
       "      <td>男</td>\n",
       "      <td>赵敏</td>\n",
       "      <td>19</td>\n",
       "      <td>165</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>张无忌</td>\n",
       "      <td>20</td>\n",
       "      <td>185</td>\n",
       "      <td>男</td>\n",
       "      <td>周芷若</td>\n",
       "      <td>21</td>\n",
       "      <td>168</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  A_name  A_age  A_height A_gender B_name  B_age  B_height B_gender\n",
       "0    张三丰    100       180        男     赵敏     19       165        女\n",
       "1    张无忌     20       185        男    周芷若     21       168        女"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person_all1 = pd.concat([person1,person2],axis=0,ignore_index=True)\n",
    "person_all1\n",
    "person_all2 = pd.concat([person1,person2],axis=1,ignore_index=True)\n",
    "person_all2\n",
    "person_all_col = pd.concat([person1, person2], axis=1)\n",
    "person_all_col\n",
    "person_all_col_prefix = pd.concat(\n",
    "    [person1.add_prefix('A_'), person2.add_prefix('B_')],  # 给person1列加A_前缀，person2加B_前缀\n",
    "    axis=1\n",
    ")\n",
    "person_all_col_prefix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "bb818ff4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>商品名称</th>\n",
       "      <th>出售日期</th>\n",
       "      <th>价格(元)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>荣耀9X</td>\n",
       "      <td>5月25日</td>\n",
       "      <td>999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>小米6X</td>\n",
       "      <td>5月25日</td>\n",
       "      <td>1399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>OPPO A1</td>\n",
       "      <td>5月25日</td>\n",
       "      <td>1399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>荣耀9X</td>\n",
       "      <td>6月18日</td>\n",
       "      <td>800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>小米6X</td>\n",
       "      <td>6月18日</td>\n",
       "      <td>1200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>OPPO A1</td>\n",
       "      <td>6月18日</td>\n",
       "      <td>1250</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      商品名称   出售日期  价格(元)\n",
       "0     荣耀9X  5月25日    999\n",
       "1     小米6X  5月25日   1399\n",
       "2  OPPO A1  5月25日   1399\n",
       "3     荣耀9X  6月18日    800\n",
       "4     小米6X  6月18日   1200\n",
       "5  OPPO A1  6月18日   1250"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_sales = pd.DataFrame({\n",
    "    '商品名称': ['荣耀9X', '小米6X', 'OPPO A1', '荣耀9X', '小米6X', 'OPPO A1'],\n",
    "    '出售日期': ['5月25日', '5月25日', '5月25日', '6月18日', '6月18日', '6月18日'],\n",
    "    '价格(元)': [999, 1399, 1399, 800, 1200, 1250]\n",
    "})\n",
    "\n",
    "df_sales"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "557d2cbe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>商品名称</th>\n",
       "      <th>OPPO A1</th>\n",
       "      <th>小米6X</th>\n",
       "      <th>荣耀9X</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>出售日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5月25日</th>\n",
       "      <td>1399</td>\n",
       "      <td>1399</td>\n",
       "      <td>999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6月18日</th>\n",
       "      <td>1250</td>\n",
       "      <td>1200</td>\n",
       "      <td>800</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "商品名称   OPPO A1  小米6X  荣耀9X\n",
       "出售日期                      \n",
       "5月25日     1399  1399   999\n",
       "6月18日     1250  1200   800"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pivot = df_sales.pivot(index='出售日期', columns='商品名称', values='价格(元)')\n",
    "df_pivot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "657644e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['OPPO A1', '小米6X', '荣耀9X']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pivot.columns.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "252c3e2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'出售日期'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pivot.index.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "797093c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>出售日期</th>\n",
       "      <th>商品名称</th>\n",
       "      <th>价格(元)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5月25日</td>\n",
       "      <td>OPPO A1</td>\n",
       "      <td>1399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>6月18日</td>\n",
       "      <td>OPPO A1</td>\n",
       "      <td>1250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5月25日</td>\n",
       "      <td>小米6X</td>\n",
       "      <td>1399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6月18日</td>\n",
       "      <td>小米6X</td>\n",
       "      <td>1200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5月25日</td>\n",
       "      <td>荣耀9X</td>\n",
       "      <td>999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6月18日</td>\n",
       "      <td>荣耀9X</td>\n",
       "      <td>800</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    出售日期     商品名称  价格(元)\n",
       "0  5月25日  OPPO A1   1399\n",
       "1  6月18日  OPPO A1   1250\n",
       "2  5月25日     小米6X   1399\n",
       "3  6月18日     小米6X   1200\n",
       "4  5月25日     荣耀9X    999\n",
       "5  6月18日     荣耀9X    800"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pivot_reset = df_pivot.reset_index()\n",
    "df_melt = df_pivot_reset.melt(\n",
    "    id_vars=['出售日期'],  # 保留的行索引（转为列）\n",
    "    var_name='商品名称',    # 原列索引的新列名\n",
    "    value_name='价格(元)'   # 原数值的新列名\n",
    ")\n",
    "df_melt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "495bd99a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>商品类别</th>\n",
       "      <th>商品名称</th>\n",
       "      <th>销量</th>\n",
       "      <th>单价(元)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>手机</td>\n",
       "      <td>荣耀9X</td>\n",
       "      <td>100</td>\n",
       "      <td>999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>手机</td>\n",
       "      <td>小米6X</td>\n",
       "      <td>80</td>\n",
       "      <td>1399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>电脑</td>\n",
       "      <td>联想小新</td>\n",
       "      <td>50</td>\n",
       "      <td>4999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>电脑</td>\n",
       "      <td>华为MateBook</td>\n",
       "      <td>30</td>\n",
       "      <td>6999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>手机</td>\n",
       "      <td>OPPO A1</td>\n",
       "      <td>70</td>\n",
       "      <td>1399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>电脑</td>\n",
       "      <td>苹果MacBook</td>\n",
       "      <td>20</td>\n",
       "      <td>9999</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  商品类别        商品名称   销量  单价(元)\n",
       "0   手机        荣耀9X  100    999\n",
       "1   手机        小米6X   80   1399\n",
       "2   电脑        联想小新   50   4999\n",
       "3   电脑  华为MateBook   30   6999\n",
       "4   手机     OPPO A1   70   1399\n",
       "5   电脑   苹果MacBook   20   9999"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_sales_group = pd.DataFrame({\n",
    "    '商品类别': ['手机', '手机', '电脑', '电脑', '手机', '电脑'],\n",
    "    '商品名称': ['荣耀9X', '小米6X', '联想小新', '华为MateBook', 'OPPO A1', '苹果MacBook'],\n",
    "    '销量': [100, 80, 50, 30, 70, 20],\n",
    "    '单价(元)': [999, 1399, 4999, 6999, 1399, 9999]\n",
    "})\n",
    "\n",
    "df_sales_group"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ed7458ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "手机组：\n",
      "['荣耀9X', '小米6X', 'OPPO A1']\n",
      "\n",
      "电脑组：\n",
      "['联想小新', '华为MateBook', '苹果MacBook']\n"
     ]
    }
   ],
   "source": [
    "group_by_type = df_sales_group.groupby('商品类别')\n",
    "\n",
    "for group_name, group_data in group_by_type:\n",
    "    print(f'\\n{group_name}组：')\n",
    "    print(group_data['商品名称'].tolist())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "3e2ae829",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "agg多函数聚合结果：\n",
      "       销量              单价(元)\n",
      "      sum       mean  median\n",
      "商品类别                        \n",
      "手机    250  83.333333  1399.0\n",
      "电脑    100  33.333333  6999.0\n"
     ]
    }
   ],
   "source": [
    "agg_result = group_by_type.agg({\n",
    "    '销量': ['sum', 'mean'],  # 销量：求和+均值\n",
    "    '单价(元)': 'median'      # 单价：中位数\n",
    "})\n",
    "print(\"\\nagg多函数聚合结果：\")\n",
    "print(agg_result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py39",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.23"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
